﻿<?php

/**
 * This is the model class for table "theme".
 *
 * The followings are the available columns in table 'theme':
 * @property string $id
 * @property string $parent_id
 * @property string $prev
 * @property string $next
 * @property string $title
 * @property string $body
 * @property string $meta_keywords
 * @property string $meta_description
 *
 * The followings are the available model relations:
 * @property Theme $parent
 * @property Theme[] $themes
 */
class Theme extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Theme the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'theme';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('parent_id, prev, next', 'length', 'max'=>11),
			array('title, meta_keywords, meta_description', 'length', 'max'=>255),
			array('body', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, parent_id, prev, next, title, body, meta_keywords, meta_description', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			//'parent' => array(self::BELONGS_TO, 'Theme', 'parent_id'),
			//'themes' => array(self::HAS_MANY, 'Theme', 'parent_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'parent_id' => 'Батьківська тема',
			'prev' => 'Минула тема',
			'next' => 'Наступна тема',
			'title' => 'Заголовок',
			'body' => 'Зміст',
			//'meta_keywords' => 'Ключевые слова (SEO)',
		    //'meta_description' => 'Описание (SEO)',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('parent_id',$this->parent_id,true);
		$criteria->compare('prev',$this->prev,true);
		$criteria->compare('next',$this->next,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('body',$this->body,true);
		$criteria->compare('meta_keywords',$this->meta_keywords,true);
		$criteria->compare('meta_description',$this->meta_description,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public static function getParentThemes(){
		$sql = Yii::app()->db->createCommand()
				->select()
				->from('theme')
				->queryAll();
		if(!empty($sql)){
			$arr = array();
			foreach ($sql as $sq){
				$arr[$sq['id']] = $sq['title'];
			}
		}
		return $arr;
	}
}